define(['jquery','mylayer','loginReg'], function ($,mylayer,md_loginReg) {
	//判断是否存在
    $.isInArray=function(data,keyword){
        var result=-1;//不存在返回-1
        keyword=keyword.toLowerCase();//小写
        $.each(data,function(key,value){
            if(value.keyword.toLowerCase()==keyword){
                result=key;//存在返回索引
                return false;
            }
        });
        return result;
    };
    //ajaxpost请求
    $.postAjax=function(url,data,success,error){
    	$.ajax({url:url,data:data,type:"post",dataType:"json",success:function(result){
			if(typeof success =="function"){success(result);}
			},error:function(){
					if(typeof error =="function"){error();}
			}
		});
    };
    $.numbertranx=function($num){
    	var $number=parseInt($num)?parseInt($num):0;
    	if($number<999){
    		return $number;
    	}
    	else if($number<999999){
    		$number=$number/1000;
    		$number=$number.toFixed(2);
    		return $number.substring(0,$number.lastIndexOf('.')+2)+"k"; 
    	}
    	else{
    		$number=$number/1000000;
    		$number=$number.toFixed(2);
    		return $number.substring(0,$number.lastIndexOf('.')+2)+"m"; 
    	}
    }
    $.getScript = function(url, callback, cache) {
		$.ajax({type: 'GET', url: url, success: callback, dataType: 'script', ifModified: true, cache:cache||false});
	};
	var app={
		//公共事件
		init:function(){
			var _this=this;
			//显示已经搜索的名称
			if($("#currentkeyword").size()>0){
				$(".searchArea").val($("#currentkeyword").val());
			}
			//滚动到顶部
			$(".to_top").on("click",function(){
				_this.scroll('0px', 300);
			});
			//滚动显示向上滚动按钮
			window.onscroll=function(){
				if($(".to_top").size()>0){
					if(_this.getScrollTop()>400){
			   			$(".to_top").show();
				   	}
				   	else{
				   		$(".to_top").hide();
				   	}
				}
			};
			//加载发布笔记
			$(".upload_post").on("click",function(){
				_this.uploadPost(this);
			});
			//处理浏览器回退刷新
			var site_url=window.location.href;
			$(window).on('popstate', function(e){
				if(window.location.href!=site_url){
					window.location.reload();
				}
			});
			require.config({ urlArgs: "v=" + (new Date()).getTime() });
			require(['search'],function(md_search){
				md_search.init();
			});
		},
		uploadPost:function(obj,success){
			var that=this;
			var index=mylayer.showLoad(true);
			var loadUpload=function(md_post_add){
				if($(".layer_post_add").size()>0){
					mylayer.closeLayer(index);
					//显示发布笔记
					$(".overlay_layer_post_add").show();
					$(".layer_post_add").show();
					$("html,body").css("overflow-y", "hidden");
				}
				else{
					$.postAjax("/post/load",{},function(result){
						mylayer.closeLayer(index);
						if(result.code=="0x00001"){
							md_loginReg.showLogin();//显示登录
							//设置登录完成事件
							md_loginReg.initSuccess(function(){
								$(obj)[0].click();
							});
							return false;
						}
						if(result.code=="0x00000"){
							//显示发布笔记
							$(".overlay_layer_post_add").remove();
							$(".layer_post_add").remove();
							var lay=mylayer.init({
								title:'',
								content:result.data,
								isHeight:true,
								closeFixed:true,
								class_name:"layer_post_add",
								phoneCover:true,
								layerClose:true,
								success:function(){
									$("html,body").css("overflow-y","hidden");
									if(typeof md_post_add!="undefined"){
										var op={success:success};
										md_post_add.init(op);
									}
								},
								no_remove:true,
								closeEvent:function(){
									$("html,body").css("overflow-y","auto");
								}
							});
							$("#layerBox"+lay).css({'top':"10px","margin-top":"0px"});
						}
					});
				}
			}
			require(['post_add'], function (md_post_add){
		　　　　loadUpload(md_post_add);
		　　});
		},
		scroll:function(scrollTo, time){
			var scrollFrom = parseInt(document.body.scrollTop?document.body.scrollTop:document.documentElement.scrollTop),
			    i = 0,
			    runEvery = 5; 
			    scrollTo = parseInt(scrollTo);
			    time /= runEvery;
			    var interval = setInterval(function () {
		        i++;
		        if(document.body.scrollTop){
		        	document.body.scrollTop = (scrollTo - scrollFrom) / time * i + scrollFrom;
		        }
		        else{
		        	document.documentElement.scrollTop=(scrollTo - scrollFrom) / time * i + scrollFrom;
		        }
		        if (i >= time) {
		            clearInterval(interval);
		        }
			}, runEvery);
		},
		getScrollTop:function(){
			var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
		　  if(document.body){
		　　　　bodyScrollTop = document.body.scrollTop;
		　　}
		　　if(document.documentElement){
		　　　　documentScrollTop = document.documentElement.scrollTop;
		　　}
		　　scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
		　　return scrollTop;
		},
		getScrollHeight:function(){
			var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
		　　if(document.body){
		　　　　bodyScrollHeight = document.body.scrollHeight;
		　　}
		　　if(document.documentElement){
		　　　　documentScrollHeight = document.documentElement.scrollHeight;
		　　}
		　　scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
		　　return scrollHeight;
		},
		getWindowHeight:function(){
			var windowHeight = 0;
		　　if(document.compatMode == "CSS1Compat"){
		　　　　windowHeight = document.documentElement.clientHeight;
		　　}else{
		　　　　windowHeight = document.body.clientHeight;
		　　}
		　　return windowHeight;
		}
	};
	if(typeof app!=undefined){
		$(function(){
			app.init();
		});
	}
	return app;
});
